<?php
// 本类由系统自动生成，仅供测试用途
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {

  function p($data){
      echo '<pre>';
      print_r($data);
      echo '</pre>';
  }

    public function index(){
      header("Content-Type:text/html;charset=utf-8");

      function getNeedBetween($kw1,$mark1,$mark2){
          $kw=$kw1;
          $st =stripos($kw,$mark1);
          $ed =stripos($kw,$mark2);
          if(($st==false||$ed==false)||$st>=$ed)
          return 0;
          $kw=substr($kw,($st+1),($ed-$st-1));
          return $kw;
      }

      $host = "https://ali-weather.showapi.com";
      $path = "/ip-to-weather";
      $method = "GET";
      $appcode = "1828fefd3a40452d98bc47de5daf30e1";
      $headers = array();
      array_push($headers, "Authorization:APPCODE " . $appcode);
      $querys = "ip=202.98.20.177&need3HourForcast=0&needAlarm=1&needHourData=0&needIndex=1&needMoreDay=1";
      $bodys = "";
      $url = $host . $path . "?" . $querys;

      $curl = curl_init();
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
      curl_setopt($curl, CURLOPT_URL, $url);
      curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
      curl_setopt($curl, CURLOPT_FAILONERROR, false);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_HEADER, true);
      if (1 == strpos("$".$host, "https://"))
      {
          curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
          curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
      }

      // var_dump(curl_exec($curl));

      // $json = curl_exec($curl);
      // var_dump($json);

      // $exp = explode("{",$json);
      // p($exp);

      // $arr = json_decode($json,true);
      // var_dump($arr);
      $json = curl_exec($curl);
      // var_dump($json);

      $all="{\"".getNeedBetween($json, '"showapi_res_code"' , '}}}' )."}}}";
      // var_dump($all);
      $arr = json_decode($all,true);
      // p($arr);
      // die();

      $data = array(
          'time' => $arr['showapi_res_body']['time'], //预报发布时间
          'country' => $arr['showapi_res_body']['cityInfo']['c9'],
          'province' => $arr['showapi_res_body']['cityInfo']['c7'],
          'city' => $arr['showapi_res_body']['cityInfo']['c3'],
          //现在实时的天气情况
          'now' => array(
              'wind_direction' => $arr['showapi_res_body']['now']['wind_direction'],
              'wind_power' => $arr['showapi_res_body']['now']['wind_power'],
              'sd' => $arr['showapi_res_body']['now']['sd'], //空气湿度
              'weather_pic' => $arr['showapi_res_body']['now']['weather_pic'],//天气小图标
              'weather' => $arr['showapi_res_body']['now']['weather'], //天气
              'temperature' => $arr['showapi_res_body']['now']['temperature'],//气温
              ),
          'today' => array(
              'day' => $arr['showapi_res_body']['f1']['day'],//当前天
              'weekday' => $arr['showapi_res_body']['f1']['weekday'], //星期几
              'day_weather' => $arr['showapi_res_body']['f1']['day_weather'], //白天天气
              'day_weather_pic' => $arr['showapi_res_body']['f1']['day_weather_pic'],//白天天气图标
              'day_air_temperature' => $arr['showapi_res_body']['f1']['day_air_temperature'],//白天天气温度(摄氏度)
              'night_weather' => $arr['showapi_res_body']['f1']['night_weather'],//晚上天气
              'night_weather_pic' => $arr['showapi_res_body']['f1']['night_weather_pic'],//晚上天气图标
              'night_air_temperature' =>  $arr['showapi_res_body']['f1']['night_air_temperature'], //晚上天气温度(摄氏度)
              'ls' => array(//晾晒指数
                  'title' => $arr['showapi_res_body']['f1']['index']['ls']['title'],
                  'desc' => $arr['showapi_res_body']['f1']['index']['ls']['desc'],
                  ),
              'clothes' => array(//穿衣指数
                  'title' => $arr['showapi_res_body']['f1']['index']['clothes']['title'],
                  'desc' => $arr['showapi_res_body']['f1']['index']['clothes']['desc'],
                  ),
              'beauty' => array(//化妆指数
                  'title' => $arr['showapi_res_body']['f1']['index']['beauty']['title'],
                  'desc' => $arr['showapi_res_body']['f1']['index']['beauty']['desc'],
                  ),
              'gj' => array(//逛街指数
                  'title' => $arr['showapi_res_body']['f1']['index']['gj']['title'],
                  'desc' => $arr['showapi_res_body']['f1']['index']['gj']['desc'],
                  ),
              'uv' => array(//紫外线
                  'title' => $arr['showapi_res_body']['f1']['index']['uv']['title'],
                  'desc' => $arr['showapi_res_body']['f1']['index']['uv']['desc'],
                  ),
              'aqi' => array(//空气质量指数，越小越好
                  'num' =>  $arr['showapi_res_body']['now']['aqi'],
                  'title' => $arr['showapi_res_body']['f1']['index']['aqi']['title'],
                  'desc' => $arr['showapi_res_body']['f1']['index']['aqi']['desc'],
                  ),
              'jiangshui' => $arr['showapi_res_body']['f1']['jiangshui'],//降水概率
              ),
          'tomorrow' => array(
              //同today
              'day' => $arr['showapi_res_body']['f2']['day'],//当前天
              'weekday' => $arr['showapi_res_body']['f2']['weekday'], //星期几
              'day_weather' => $arr['showapi_res_body']['f2']['day_weather'], //白天天气
              'day_weather_pic' => $arr['showapi_res_body']['f2']['day_weather_pic'],//白天天气图标
              'day_air_temperature' => $arr['showapi_res_body']['f2']['day_air_temperature'],//白天天气温度(摄氏度)
              'night_weather' => $arr['showapi_res_body']['f2']['night_weather'],//晚上天气
              'night_weather_pic' => $arr['showapi_res_body']['f2']['night_weather_pic'],//晚上天气图标
              'night_air_temperature' =>  $arr['showapi_res_body']['f2']['night_air_temperature'], //晚上天气温度(摄氏度)
              'ls' => array(//晾晒指数
                  'title' => $arr['showapi_res_body']['f2']['index']['ls']['title'],
                  'desc' => $arr['showapi_res_body']['f2']['index']['ls']['desc'],
                  ),
              'clothes' => array(//穿衣指数
                  'title' => $arr['showapi_res_body']['f2']['index']['clothes']['title'],
                  'desc' => $arr['showapi_res_body']['f2']['index']['clothes']['desc'],
                  ),
              'beauty' => array(//化妆指数
                  'title' => $arr['showapi_res_body']['f2']['index']['beauty']['title'],
                  'desc' => $arr['showapi_res_body']['f2']['index']['beauty']['desc'],
                  ),
              'gj' => array(//逛街指数
                  'title' => $arr['showapi_res_body']['f2']['index']['gj']['title'],
                  'desc' => $arr['showapi_res_body']['f2']['index']['gj']['desc'],
                  ),
              'uv' => array(//紫外线
                  'title' => $arr['showapi_res_body']['f2']['index']['uv']['title'],
                  'desc' => $arr['showapi_res_body']['f2']['index']['uv']['desc'],
                  ),
              'aqi' => array(//空气质量指数，越小越好
                  'title' => $arr['showapi_res_body']['f2']['index']['aqi']['title'],
                  'desc' => $arr['showapi_res_body']['f2']['index']['aqi']['desc'],
                  ),
              'jiangshui' => $arr['showapi_res_body']['f2']['jiangshui'],//降水概率
              ),
          );

      /**
       * 总输出
       */
      // p($data);
      /**
       * 走前端
       */
      // p($data);
      $this->year = substr($data['time'],0,4);
      $this->month = substr($data['time'],4,2);
      $this->day = substr($data['time'],6,2);
      $this->hour = substr($data['time'],8,2);
      $this->min = substr($data['time'],10,2);
      $this->s = substr($data['time'],12,2);
      $this->tYear = substr($data['tomorrow']['day'],0,4);
      $this->tMonth = substr($data['tomorrow']['day'],4,2);
      $this->tDay = substr($data['tomorrow']['day'],6,2);


      // $this->p($data);
      // die();
      $this->assign('weather',$data);
      $this->display();


      // $arr = json_decode($json,true);
      // var_dump($arr);

      // $weather_code="{\"".getNeedBetween($json, '"weather_code"' , ',"f1' );
      // $cityInfo="{\"".getNeedBetween($json, '"cityInfo"' , ',"now' )."}";
      // $f1="{\"".getNeedBetween($json, '"f1"' , ',"air_press' )."}";
      // var_dump($f1);
      // $arr = json_decode($f1,true);
      // p($arr);
    }
}
